/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.Admin;

import Models.Account;
import Models.Address;
import Models.Books;
import Models.DAOs.Account_DAO;
import Models.DAOs.Book_DAO;
import Security.Encrypter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Don
 */
public class admin_action extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            Enumeration enumerateParameters = request.getParameterNames();
            int count = 0;
            while(enumerateParameters.hasMoreElements())
            {
                enumerateParameters.nextElement();
                count++;
            }
            if(count == 0){
               response.sendRedirect("admin_manager");
            }
            else{              
                boolean isUserAction = false;
                String confirmMsg = null;
                if(request.getParameterMap().containsKey("form") && request.getParameter("form").equals("$#!@e")){
                    Encrypter encrypt = new Encrypter();
                    String userID = encrypt.decrypt(request.getParameter("user"));
                    isUserAction = new Account_DAO().updateAccountStatus(Integer.parseInt(userID), 0);
                    if(isUserAction)
                        confirmMsg = "The user was successfully removed.";
                    else
                        confirmMsg = "Failed to remove user.";
                }
                else{
                    Encrypter encrypt = new Encrypter();
                    Account account = new Account();
                    boolean isUsername = false;
                    account.setFirstName(request.getParameter("firstName"));
                    account.setMidName(request.getParameter("midName"));
                    account.setLastName(request.getParameter("lastName"));
                    account.setEmail(request.getParameter("email"));
                    account.setUsername(request.getParameter("username"));
                    account.setPassword(request.getParameter("password"));
                    account.setUserType(encrypt.decrypt(request.getParameter("ut")));
                    System.out.println(account.getUserType());
                    isUsername = new Account_DAO().checkUsername(request.getParameter("username"));
                    if(!isUsername){
                        if(request.getParameter("formType").equals("a@#!@qw")){
                            isUserAction = new Account_DAO().addAccount(account,request.getParameter("repassword"));
                            if(isUserAction)
                                confirmMsg = "The user "+request.getParameter("lastName")+" was successfully added.";
                            else
                                confirmMsg = "Failed to add user "+request.getParameter("lastName") +".";
                        }
                    }
                    else
                        confirmMsg = "Username already exists.";
               
                }
                request.setAttribute("confirmMsg", confirmMsg);      
                RequestDispatcher rd = request.getRequestDispatcher("admin_home");
                rd.forward(request, response);
            }
           
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
